import { Component, OnInit } from '@angular/core';
import { HttpService } from 'src/app/service/http.service';
import { UrlService } from 'src/app/service/url.service';
import { ConfigService } from 'src/app/service/config.service';
import { PopwinService } from 'src/app/service/popwin.service';
import { ToolService } from 'src/app/service/tool.service';
import { NavController } from '@ionic/angular';
import { EventService } from 'src/app/service/event.service';
import { Storage } from '@ionic/storage';
import { Router } from '@angular/router';

@Component({
  selector: 'app-index',
  templateUrl: './index.page.html',
  styleUrls: ['./index.page.scss'],
})
export class IndexPage implements OnInit {
  menuData = { "zhcz": "", "tx": "", "czjl": "" };
  public money;
  public isHideMoney = false;

  public isThird=false;//是否有第三方
  constructor(private http: HttpService, private popWin: PopwinService, private navCtrl: NavController, private eventS: EventService, private store: Storage, private router: Router) {
    this.isThird=ConfigService.is3rd;
   }

  ngOnInit() {
    this.initEvent();
  }

  initEvent() {
    let that = this;
    this.eventS.subscribe("selectCommonTab2", function (index) {
      if (index == 2) {
        that.showMenu();
        that.showTotalMoney();
        that.initShowMoney();//余额是否隐藏
      }
    }, true);
    this.eventS.subscribe("updateTotalMoney",function(){
       that.showTotalMoney();
    },true);
  }

  /**
   * 触发显示或隐藏余额
   */
  showMoney() {
    let that = this;
    this.store.get("isHideMoney").then((data) => {
      if (data == '1') {
        that.store.set("isHideMoney", '0');
        that.isHideMoney = false;
      } else {
        that.store.set("isHideMoney", '1');
        that.isHideMoney = true;
      }
    });
  }
  /**
   * 初始化余额是否显示或隐藏
   */
  initShowMoney() {
    let that = this;
    this.store.get("isHideMoney").then((data) => {
      if (data == '1') {
        that.isHideMoney = true;
      } else {
        that.isHideMoney = false;
      }
    });
  }

  /**
   * 入口是否做隐藏处理
   */
  showMenu() {
    let that = this;
    this.http.get(UrlService.FILE_RECHARGEWITHDRAWAL, { "callback": "JSONP_CALLBACK" }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      if (data.code == ConfigService.SUCCESS_CODE) {
        let temp = data.data;
        let tt = ToolService.aesDecodeToJson(temp);
        that.menuData = tt;
        console.log(tt);
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
        that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
    });
  }

  async  toWithdraw() {//是否可提现的判断
    let that = this;
    let tempIsLogin;
    await this.store.get(ConfigService.IS_LOGIN_STORE_KEY).then((data) => {
      tempIsLogin = data;
    });
    if (tempIsLogin != '1' && tempIsLogin != 1) {
      this.navCtrl.navigateForward("/login");
      return;
    }
    let roleStr;
    await this.store.get(ConfigService.ROLE_KEY).then((data) => {
      roleStr = data;
    });
    if (roleStr == '2') {
      that.popWin.toast("试玩账号不能进入");
      return;
    }
    this.http.get(UrlService.USER_SHOWACCTOUT, { "callback": "JSONP_CALLBACK" }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      if (data.code == ConfigService.SUCCESS_CODE) {
        let temp = data.data;
        let tt = ToolService.aesDecodeToJson(temp);
        if (!ToolService.isEmpty(tt.outcashmsg)) {
          if(tt.reason=='cash'){
            that.popWin.alert(tt.outcashmsg, "万漉国际提示");
            return;
          }
          that.popWin.alertOkAndCancel(tt.outcashmsg, "万漉国际提示", "", function () {
            //取消
          }, function () {
            //确定
            that.navCtrl.navigateForward("/personalcenter");
          });
          return;
        }
        that.navCtrl.navigateForward("/withdraw");
        console.log("--", tt);
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
        that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
    });
  }

  /**
   * 跳转到充值记录中
   */
  async toReport(typeStr) {
    let tempIsLogin;
    let that = this;
    await this.store.get(ConfigService.IS_LOGIN_STORE_KEY).then((data) => {
      tempIsLogin = data;
    });
    if (tempIsLogin == '1' || tempIsLogin == 1) {
      this.store.get(ConfigService.ROLE_KEY).then((data) => {
        if (data == '2') {
          that.popWin.toast("试玩账号不能进入");
          return;
        }
        if (typeStr == '1') {
          let param = "{\"name\":\"充值\",\"type\":\"1\"}";
          this.router.navigate(['/transaction'], {
            queryParams: { "p": ToolService.paramAESEncrypt(param)}
          });
        } else if (typeStr == '2') {
          let param = "{\"name\":\"提现\",\"type\":\"2\"}";
          this.router.navigate(['/transaction'], {
            queryParams: { "p": ToolService.paramAESEncrypt(param)}
          });
        }
      });
    } else {
      this.navCtrl.navigateForward("/login");
    }

  }



  async toAccountrecharge() {
    let that = this;
    let tempIsLogin;
    await this.store.get(ConfigService.IS_LOGIN_STORE_KEY).then((data) => {
      tempIsLogin = data;
    });
    if (tempIsLogin == '1' || tempIsLogin == 1) {
      this.store.get(ConfigService.ROLE_KEY).then((data) => {
        if (data == '2') {
          that.popWin.toast("试玩账号不能进入");
          return;
        }
        that.navCtrl.navigateForward("/accountrecharge");
      });
    } else {
      this.navCtrl.navigateForward("/login");
    }
  }

  /**
  * 获取资产并存储数据
  */
  async showTotalMoney() {
    let that = this;
    let tempMoney;
    await this.store.get(ConfigService.MONEY_KEY).then((data) => {
      tempMoney = data;
    });
    if (!ToolService.isEmpty(tempMoney)) {
      let temp = tempMoney;
      that.money = ToolService.keepTwoDecimal(temp.credit2);
      return;
    }
    this.http.get(UrlService.USER_FINDCREDIT, { "callback": "JSONP_CALLBACK" }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      if (data.code == ConfigService.SUCCESS_CODE) {
        let res = ToolService.aesDecodeToJson(data.data);
        that.money = ToolService.keepTwoDecimal(res.credit2);
        that.store.set(ConfigService.MONEY_KEY, res);
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
        that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
    });
  }

async  toQuotaPage(){
  let that=this;
  let isTestPlay=false;
  await  this.store.get(ConfigService.ROLE_KEY).then((data) => {
      if (data == '2') {
        isTestPlay=true;
      }
    });
    if(isTestPlay){
      that.popWin.toast("试玩账号不能进入");
      return;
    }
    this.navCtrl.navigateForward("/quota");
  }

  async toWalletPage(){
    let that=this;
    let isTestPlay=false;
    await  this.store.get(ConfigService.ROLE_KEY).then((data) => {
        if (data == '2') {
          isTestPlay=true;
        }
      });
      if(isTestPlay){
        that.popWin.toast("试玩账号不能进入");
        return;
      }
      this.navCtrl.navigateForward("/wallet");
  }
}
